package com.websai.servlets;

import com.sai.clases.Alumno;
import com.sai.utils.Utilities;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class Alumnos extends HttpServlet {

    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        response.setContentType("text/html;charset=UTF-8");
        PrintWriter out = response.getWriter();
        try {
            int metodo = Integer.parseInt(request.getParameter("metodo"));
            switch(metodo){
                case 0:
                    Registrar(request, response);
                    break;
                case 1:
                    Modificar(request, response);
                    break;
                case 2:
                    Eliminar(request, response);
                    break;
            }
        } finally {            
            out.close();
        }
    }
    
    public void Registrar(HttpServletRequest request, HttpServletResponse response) throws IOException {
        response.setContentType("text/html;charset=UTF-8");
        PrintWriter out = response.getWriter();
        
        try {
            String nombre = request.getParameter("Nombre");
            String ap = request.getParameter("APaterno");
            String am = request.getParameter("AMaterno");
            String matricula = request.getParameter("Matricula");
            String grupo = request.getParameter("Grupo");
            String proyecto = request.getParameter("Materia");
            
            if(Utilities.isReallyEmptyOrNull(nombre) && Utilities.isReallyEmptyOrNull(ap) && Utilities.isReallyEmptyOrNull(am)
                    && Utilities.isReallyEmptyOrNull(matricula) && Utilities.isReallyEmptyOrNull(grupo)
                    && Utilities.isReallyEmptyOrNull(proyecto)){
                out.print("Blanco");
            } else if(Utilities.isReallyEmptyOrNull(nombre)) {
                out.print("Nombre");
            } else if(Utilities.isReallyEmptyOrNull(ap)) {
                out.print("AP");
            } else if(Utilities.isReallyEmptyOrNull(am)) {
                out.print("AM");
            } else if(Utilities.isReallyEmptyOrNull(matricula)) {
                out.print("Matricula");
            } else if(Utilities.isReallyEmptyOrNull(grupo)) {
                out.print("Grupo");
            } else if(Utilities.isReallyEmptyOrNull(proyecto)) {
                out.print("Proyecto");
            } else {
                Alumno alumno = new Alumno();
                alumno.setNombre(nombre);
                alumno.setApellidoP(ap);
                alumno.setApellidoM(am);
                alumno.setMatricula(matricula);
                alumno.setIdGrupo(grupo);
                alumno.setNombreProyecto(proyecto);
                
                String respuesta = null;
                if(respuesta.equals("OK")){
                    out.print("OK");
                } else {
                    out.print(respuesta);
                }
            } 
        } finally {
            out.close();
        }
    }
    
    public void Modificar(HttpServletRequest request, HttpServletResponse response) throws IOException {
        response.setContentType("text/html;charset=UTF-8");
        PrintWriter out = response.getWriter();
        
        try {
            String id = request.getParameter("Id");
            
            if(Utilities.isReallyEmptyOrNull(id)){
                out.print("Id");
            } else {
                Alumno alumno = new Alumno();
                alumno.setIdAlumno(Integer.parseInt(id));
                
                String respuesta = null;
                if(respuesta.equals("OK")){
                    out.print("OK");
                } else {
                    out.print(respuesta);
                }
            }
        } finally {
            out.close();
        }
    }
    
    public void Eliminar(HttpServletRequest request, HttpServletResponse response) throws IOException {
        response.setContentType("text/html;charset=UTF-8");
        PrintWriter out = response.getWriter();
        
        try {
            String id = request.getParameter("Id");
            
            if(Utilities.isReallyEmptyOrNull(id)){
                out.print("Id");
            } else {
                Alumno alumno = new Alumno();
                alumno.setIdAlumno(Integer.parseInt(id));
                
                String respuesta = null;
                if(respuesta.equals("OK")){
                    out.print("OK");
                } else {
                    out.print(respuesta);
                }
            }
        } finally {
            out.close();
        }
    }
    
    public void Consultar(HttpServletRequest request, HttpServletResponse response) throws IOException {
        response.setContentType("text/html;charset=UTF-8");
        PrintWriter out = response.getWriter();
        
        try {
            out.print(out);
        } finally {
            out.close();
        }
    }

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /** 
     * Handles the HTTP <code>GET</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /** 
     * Handles the HTTP <code>POST</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /** 
     * Returns a short description of the servlet.
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>
}
